package com.sinodata.common
{
	import com.sinodata.workflow.Task;
	import com.sinodata.workflow.WorkFlow;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.events.FlexEvent;
	import com.sinodata.components.SurfaceContainer;

	
	public class Surface extends Canvas implements ISurface
	{	
		[Bindable]
		private var viewData:Object = new Object();
		
		[Bindable]
		public var timeout:Timeout = new Timeout();
		
		private var timer:Timer = new Timer(1000, 0);
		
		public function Surface()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(FlexEvent.REMOVE,destroy);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,initView);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,startTimer);
			
		}
		public function setData(value:Object):void
		{
			this.viewData = value;
		}
		public function getData():Object
		{
			return this.viewData;
		}
		
		public final function initView(event:Event):void{ //根据
			if(task != null){
				if(task.config != null){
					for(var config:String in task.config){
						if(this.hasOwnProperty(config)){
							var control:Object = this[config];
							var cc:Object = task.config[config];
							for(var property:String in cc){
								if(control.hasOwnProperty(property))
								{
									//int，uint，Boolean，Number，String
									var type:String = typeof(control[property]);
									if(type == "int")
										control[property] = int(cc[property]);
									else if(type == "uint")
										control[property] = uint(cc[property]);
									else if(type == "boolean")
										control[property] = cc[property].toString().toLocaleLowerCase() == "true";
									else if(type == "number")
										control[property] = Number(cc[property]);
									else if(type == "string")
										control[property] = cc[property];
									else
										control[property] = cc[property];
									
								}
							}
						}
					}
				}
			}
		}
		
		public function init(event:Event):void
		{
			throw new Error("未实现init方法");
		}
		public function destroy(event:Event):void{
			throw new Error("未实现destroy方法");
		}
		
		public final function exec(condition:String="0",data:Object=null,direction:int=0):void{
			timer.stop();
			WorkFlow.getInstance().callback(this.task,condition,data,direction);
		} 
		
		public final function next(viewName:String,data:Object):void{
			var  sc:SurfaceContainer  = this.parent as SurfaceContainer;
			var ClassReference:Class = flash.utils.getDefinitionByName(viewName) as Class;
			var next:Surface = new ClassReference();
			next.setData(data);
			sc.ChangeView(this,next,SurfaceContainer.DIRECTION_NEXT);
		} 
		
		public  final function prior(viewName:String,data:Object):void{
			var  sc:SurfaceContainer  = this.parent as SurfaceContainer;
			var ClassReference:Class = flash.utils.getDefinitionByName(viewName) as Class;
			var next:Surface = new ClassReference();
			next.setData(data);
			sc.ChangeView(this,next,SurfaceContainer.DIRECTION_PRIOR);
		} 
		protected var task:Task;
		
		
		public function setTask(task:Task):void
		{
			this.task = task;
		}
		
		public final function startTimer(event:Event):void{
			timer.addEventListener(TimerEvent.TIMER,timeout_Listener);
			if(timeout.enable)  timer.start();
		}
		
		private function timeout_Listener (e:TimerEvent):void{
			timeout.delay--;
			if(timeout.delay == 1)
				timeout_exec();
		}
		
		public function timeout_exec():void
		{
			throw new Error("未实现timeout_exec方法");
		}
	}
}